/**
 * Yii Dialog4Input widget.
 */

(function ($) {
    $.fn.input4Query = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.input4Query');
            return false;
        }
    };

    var defaults = {
        refreshUrl: undefined,
        hashKey: undefined
    };

    var isPopuped = false;

    var methods = {
        init: function (options) {
            return this.each(function () {
                var $e = $(this);
                var settings = $.extend({}, defaults, options || {});
                $e.data('input4Query', {
                    settings: settings
                });

                $e.on('click.input4Query', function () {
                    if (isPopuped){
                        methods.closeDialog.apply($e);
                    }else {
                        methods.showDialog.apply($e);
                    }

                    isPopuped = !isPopuped;
                    return false;
                });

            });
        },
        showDialog: function () {
            var _libName = this.data('input4Query').settings.libName;
            console.log(_libName);
            $(_libName).dialog(
                "Welcome to a customized Krajee Dialog! Click the close icon on the top right to exit.",
                function(result) {
                    // do something
                }
            );
        },
        closeDialog: function (options) {
            alert('close');
            
        },
        //
        // refresh: function () {
        //     var $e = this,
        //         settings = this.data('yiiCaptcha').settings;
        //     $.ajax({
        //         url: $e.data('yiiCaptcha').settings.refreshUrl,
        //         dataType: 'json',
        //         cache: false,
        //         success: function (data) {
        //             $e.attr('src', data.url);
        //             $('body').data(settings.hashKey, [data.hash1, data.hash2]);
        //         }
        //     });
        // },
        //
        // destroy: function () {
        //     return this.each(function () {
        //         $(window).unbind('.yiiCaptcha');
        //         $(this).removeData('yiiCaptcha');
        //     });
        // },
        //
        data: function () {
            return this.data('input4Query');
        }
    };
})(window.jQuery);

